﻿using HardcoreGame.Common.Players;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Graphics;
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent;
using Terraria.ModLoader;
using Terraria.UI;

namespace HardcoreGame.Common.Systems
{
    public class StaminaSystem : ModSystem
    {
        public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
        {
            int idx = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Resource Bars"));
            if (idx != -1)
            {
                LegacyGameInterfaceLayer plrUiLayer = new("Stamina", delegate
                {
                    StaminaBarDraw();
                    return true;
                }, 0);
                layers.Insert(idx + 1, plrUiLayer);
            }
        }
        private static bool StaminaBarDraw()
        {
            Player player = Main.LocalPlayer;
            if (player.dead)
            {
                return false;
            }
            StaminaPlayer myplayer = player.GetModPlayer<StaminaPlayer>();
            SpriteBatch sb = Main.spriteBatch;
            if (sb == null)
            {
                return false;
            }
            try
            {
                if (myplayer.Logic == null)
                {
                    return false;
                }
                float alpha = myplayer.Logic.DrainingFX ? 1f : 0.65f;
                int stamina = (int)myplayer.Logic.Stamina;
                int maxStamina = myplayer.Logic.MaxStamina2;
                float fatigue = myplayer.Logic.Fatigue;
                bool isExercising = myplayer.Logic.IsExercising;
                int threshold = fatigue > 0f ? myplayer.Logic.GetStaminaLossAmountNeededForExercise() : -1;
                int plrX = (int)(player.position.X - Main.screenPosition.X) + player.width / 2;
                int plrY = (int)(player.position.Y - Main.screenPosition.Y) + player.height;
                plrX += 0;
                plrY += 0;
                DrawStaminaBar(sb, plrX, plrY, stamina, maxStamina, (int)fatigue, threshold, isExercising, alpha, 1f);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        public static void DrawStaminaBar(SpriteBatch sb, float x, float y, int stamina, int maxStamina, int fatigue, int exerciseThreshold, bool isExercising, float alpha, float scale = 1f)
        {
            Texture2D bar = TextureAssets.Hb1.Value;
            Texture2D maxbar = TextureAssets.Hb2.Value;
            int texWidth = maxbar.Width;
            int texHeight = maxbar.Height;
            float ratio = stamina / (float)maxStamina;
            if (ratio > 1f)
            {
                ratio = 1f;
            }           
            float fatRatio = fatigue / (float)maxStamina;
            if (fatRatio > 1f)
            {
                fatRatio = 1f;
            }
            float xFinal = x - texWidth * 0.5f * scale;
            float depth = 1f;
            float alphaFinal = Math.Max(0.1f, Math.Max(1f - ratio, fatRatio)) * alpha;
            float r = 255f * alphaFinal * 0.95f;
            float g = 255f * alphaFinal * 0.95f * (ratio > 0.33f ? 1f : 0f);
            float b = 0f;
            float a = 255f * alphaFinal * 0.95f;
            if (r < 0f)
            {
                r = 0f;
            }
            else if (r > 255f)
            {
                r = 255f;
            }
            if (g < 0f)
            {
                g = 0f;
            }
            else if (g > 255f)
            {
                g = 255f;
            }
            if (a < 0f)
            {
                a = 0f;
            }
            else if (a > 255f)
            {
                a = 255f;
            }
            Color color;
            color = new((byte)r, (byte)g, (byte)b, (byte)a);
            Vector2 pos;
            pos = new(xFinal, y);
            Rectangle uRect;
            uRect = new(0, 0, texWidth, texHeight);
            sb.Draw(maxbar, pos, new Rectangle?(uRect), color, 0f, default, scale, 0, depth);
            Rectangle oRect;
            oRect = new(0, 0, (int)(texWidth * ratio), bar.Height);
            sb.Draw(bar, pos, new Rectangle?(oRect), color, 0f, default, scale, 0, depth);
            if (fatigue > 0)
            {
                Color fatColor = Color.Gray;
                if (isExercising)
                {
                    fatColor = new(128, 255, 128);
                }
                int fatBarLength = (int)(texWidth * fatRatio);
                Vector2 fPos;
                fPos = new(xFinal + (texWidth - fatBarLength), y);
                Rectangle rect;
                rect = new(texWidth - fatBarLength, 0, fatBarLength, bar.Height);
                sb.Draw(bar, fPos, new Rectangle?(rect), fatColor, 0f, default, scale, 0, depth);
            }
        }
    }
}
